/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.control;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.sys.frm.BaseController;
import com.rf.sys.model.Role;
import com.rf.sys.service.RoleService;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;

@Controller
@RequestMapping("/sys/role")
public class RoleController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(RoleController.class);
	private static final long serialVersionUID = -120160623160132262L;
	
	@Resource
	private RoleService roleService;
	
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/sys/role/role_list");
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, Role role, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	Map searchMap = roleService.getRolePage(role, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
	 * 搜索页面信息--用于用户页分配角色
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "queryRole")
    @ResponseBody
    public void queryRole (PageUtils pageUtils, Role role, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String uid = request.getParameter("uid");
    	Map searchMap = roleService.getRolePageForUser(uid,role,pageUtils);
    	String s = JsonUtils.mapToJson(searchMap);
    	super.responsePrint(request, response, s);
    }
    
    /**
     * 编辑页面初始化信息
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("id");
		Role role = new Role();
		if(id != null && !id.equals("")){
			role = roleService.getRoleByID(id);	
			modelAndView.addObject("role", role);
		}else{
			role.setState(ConfigUtils.ENABLESTATE);
		}
		modelAndView.addObject("role", role);
		modelAndView.setViewName("/sys/role/role_edit");
        return modelAndView; 
    }
    
    /**
     * 保存数据信息
     * @param Role,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    public void save (Role role, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String ret = "success";
		if(role !=null && role.getId()!=null && !role.getId().equals("")){
			//修改
			roleService.updateRole(role);
		}else{
			//新增
			roleService.addRole(role);
		}
		super.resPrint(request, response, ret);//成功
    }
    
    /**
     * 删除数据信息
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String ids = request.getParameter("ids");
    	roleService.deleteRole(ids);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 更新数据状态
     * @param User,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "updateState")
    @ResponseBody
    public void updateState (Role role, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	roleService.updateRole(role);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 给角色分配菜单
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "allot")
    @ResponseBody
    public void allot (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String resid = request.getParameter("resid");
    	String roleid = request.getParameter("roleid");
    	roleService.allot(roleid, resid);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
}


